#####################################
##REPLICATION FOR MCGHEE, ET AL.,####
##"A PRIMARY CAUSE OF PARTISANSHIP"##
#####################################

#Table A9 (Appendix)#

setwd("XXXX") #ENTER DIRECTORY PATHNAME IN PLACE OF XXXX

#Democrats#
legyrd <- read.csv("legislator primaries.dem.csv", header=TRUE)

legyrd$yrcnt <- (legyrd$elec-1992)/2

legyrd$ideowt <- NULL

avg1 <- tapply(legyrd$closed, legyrd$elec, mean)
avg3 <- tapply(legyrd$semicl, legyrd$elec, mean)
avg5 <- tapply(legyrd$semiop, legyrd$elec, mean)
avg7 <- tapply(legyrd$open, legyrd$elec, mean)
avg9 <- tapply(legyrd$nonpart, legyrd$elec, mean)
avg11 <- tapply(legyrd$pred_np, legyrd$elec, mean)

legyrd <- na.omit(legyrd)

avg2 <- tapply(legyrd$closed, legyrd$elec, mean)
avg4 <- tapply(legyrd$semicl, legyrd$elec, mean)
avg6 <- tapply(legyrd$semiop, legyrd$elec, mean)
avg8 <- tapply(legyrd$open, legyrd$elec, mean)
avg10 <- tapply(legyrd$nonpart, legyrd$elec, mean)
avg12 <- tapply(legyrd$pred_np, legyrd$elec, mean)

comparison <- data.frame(closed.full=round(100*avg1), closed.prz=round(100*avg2),
				semicl.full=round(100*avg3), semicl.prz=round(100*avg4), 
				semiop.full=round(100*avg5), semiop.prz=round(100*avg6),
				open.full=round(100*avg7), open.prz=round(100*avg8),
				nonpart.full=round(100*avg9), nonpart.prz=round(100*avg10),
				ideol.full=round(avg11, 2), ideol.prz=round(avg12, 2))
comparison

#Republicans#
legyrr <- read.csv("legislator primaries.rep.csv", header=TRUE)

legyrr$yrcnt <- (legyrr$elec-1992)/2

legyrr$ideowt <- NULL

avg1 <- tapply(legyrr$closed, legyrr$elec, mean)
avg3 <- tapply(legyrr$semicl, legyrr$elec, mean)
avg5 <- tapply(legyrr$semiop, legyrr$elec, mean)
avg7 <- tapply(legyrr$open, legyrr$elec, mean)
avg9 <- tapply(legyrr$nonpart, legyrr$elec, mean)
avg11 <- tapply(legyrr$pred_np, legyrr$elec, mean)

legyrr <- na.omit(legyrr)

avg2 <- tapply(legyrr$closed, legyrr$elec, mean)
avg4 <- tapply(legyrr$semicl, legyrr$elec, mean)
avg6 <- tapply(legyrr$semiop, legyrr$elec, mean)
avg8 <- tapply(legyrr$open, legyrr$elec, mean)
avg10 <- tapply(legyrr$nonpart, legyrr$elec, mean)
avg12 <- tapply(legyrr$pred_np, legyrr$elec, mean)

comparison <- data.frame(closed.full=round(100*avg1), closed.prz=round(100*avg2),
				semicl.full=round(100*avg3), semicl.prz=round(100*avg4), 
				semiop.full=round(100*avg5), semiop.prz=round(100*avg6),
				open.full=round(100*avg7), open.prz=round(100*avg8),
				nonpart.full=round(100*avg9), nonpart.prz=round(100*avg10),
				ideol.full=round(avg11, 2), ideol.prz=round(avg12, 2))
comparison



